home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c3 / pro24 / mm.c < prev    next >
C/C++ Source or Header  |  1986-08-06  |  4KB  |  137 lines

  1. /* mm.c -- midi monitor */
  2.  
  3. /*****************************************************************************
  4. *        Change Log
  5. *  Date        | Change
  6. *-----------+-----------------------------------------------------------------
  7. *  7-Apr-86 | Created changelog
  8. *****************************************************************************/
  9.  
  10. #include "cext.h"
  11. #include "stdio.h"
  12. #include "midicode.h"
  13. #include "mpu.h"
  14. #include "cmdline.h"
  15.  
  16. int bender;    /* record pitch bend etc.? */
  17.  
  18. /****************************************************************************
  19. * Data for command line parsing
  20. ****************************************************************************/
  21. #define nswitches 8
  22. private char *switches[nswitches] = 
  23.     { "-help", "-miditrace", "-m", "-trace", "-t", "-block", "-d", "-debug"};
  24.  
  25. #define noptions 1
  26. private char *options[noptions] = { "-tune" };
  27.  
  28. /*****************************************************************************
  29. *    Routines local to this module
  30. *****************************************************************************/
  31. private    void    output();
  32. private    void    put_pitch();
  33. private    void    showhelp();
  34.  
  35. /****************************************************************************
  36. *                main
  37. * Effect: prompts for parameters, starts monitor
  38. ****************************************************************************/
  39.  
  40. void main(argc, argv)
  41.     int argc;
  42.     char *argv[];
  43. {
  44.     int done = false;
  45.     byte data[4];    /* midi input data buffer */
  46.  
  47.     cl_init(switches, nswitches, options, noptions, argv, argc);
  48.     if (cl_switch("-help")) showhelp();
  49.  
  50.     bender = askbool("pitch bend etc. on", false);
  51.  
  52.     musicinit();
  53.     while (getkey(false) != -1) ;
  54.     midi_cont(bender);
  55.     printf("Midi Monitor ready.     Type Q to stop.\n");
  56.  
  57.     while (!done) {
  58.     char c;
  59.     if (kbhit()) {
  60.         c = getch();
  61.         if (tolower(c) == 'q') done = true;
  62.         else printf("type q to quit\n");
  63.     }
  64.     if (getbuf(false, data)) {
  65.         output(data);
  66.     }
  67.     }
  68.     musicterm();
  69. }
  70.  
  71. /****************************************************************************
  72. *                output
  73. * Inputs:
  74. *    byte data[]: midi data buffer holding one command
  75. * Effect: format and print  midi data
  76. ****************************************************************************/
  77.  
  78. void output(data)
  79.     byte data[];
  80. {
  81.     int command;    /* the current command */
  82.     int chan;        /* the midi channel of the current event */
  83.  
  84.     command = data[0] & MIDI_CODE_MASK;
  85.     chan = data[0] & MIDI_CHN_MASK;
  86.  
  87.     if (command == MIDI_ON_NOTE && data[2] != 0) {
  88.     printf("Note On,  Chan %2d, Key %2d (", chan, data[1]);
  89.     put_pitch(data[1] - 12);
  90.     printf("), Vel %d\n", data[2]);
  91.     } else if (command == MIDI_ON_NOTE /* && data[2] == 0 */) {
  92.     printf("Note Off, Chan %2d, Key %2d (", chan, data[1]);
  93.     put_pitch(data[1] - 12);
  94.     printf("), Vel %d\n", data[2]);
  95.     } else if (command == MIDI_CH_PROGRAM) {
  96.     printf("Prog Chg, Chan %2d, Prog %2d\n", chan, data[1]);
  97.     } else if (command == MIDI_CTRL) {
  98.     printf("Ctrl Chg, Chan %2d, Ctrl %2d, Val %2d\n",
  99.            chan, data[1], data[2]);
  100.     } else if (command == MIDI_TOUCH) {
  101.     printf("A. Touch, Chan %2d, Val %2d\n", chan, data[1]);
  102.     } else if (command == MIDI_BEND) {
  103.     printf("P. Bend,  Chan %2d, Val %2d\n", chan,
  104.            (data[1] + (data[2]<<7)));
  105.     }
  106. }
  107.  
  108. /****************************************************************************
  109. *                put_pitch
  110. * Inputs:
  111. *    int p: pitch number
  112. * Effect: write out the pitch name for a given number
  113. ****************************************************************************/
  114.  
  115. private void put_pitch(p)
  116.     int p;
  117. {
  118.     static char *ptos[] = {"c", "cs", "d", "ef", "e", "f", "fs", "g",
  119.                "gs", "a", "bf", "b"};
  120.     printf("%s%d", ptos[p % 12], p / 12);
  121. }
  122.  
  123. /****************************************************************************
  124. *                showhelp
  125. * Effect: print help text
  126. ****************************************************************************/
  127.  
  128. private void showhelp()
  129. {
  130.     fprintf(stderr,"mm is a midi monitor program, switches are:");
  131.     fprintf(stderr,"       -block            disable MIDI thru\n");
  132.     fprintf(stderr,"       -help             this message\n");
  133.     fprintf(stderr,"       -miditrace (-m)   turn on MIDI command trace\n");
  134.     fprintf(stderr,"       -trace (-t)       trace music\n");
  135.     fprintf(stderr,"       -debug (-d)         debug flag for cintr.c\n");
  136. }
  137.